﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace DAO
{
    public class DAO_Database
    {
        private DataServices data = new DataServices();
        public bool backupDatabase(string fileName)
        {
            bool result = false;
            try
            {
                using (SqlConnection cn = new SqlConnection(data.ConnectString))
                {
                    ServerConnection svCon = new ServerConnection(cn);
                    Server svr = new Server(svCon);
                    cn.Open();
                    cn.ChangeDatabase("master");

                    //string testFolder = @"D:\";
                    string databaseName = "QuanLyGiaPha";
                    Backup backup = new Backup();
                    backup.Action = BackupActionType.Database;
                    backup.Database = databaseName;
                    backup.Incremental = false;
                    backup.Initialize = true;
                    backup.LogTruncation = BackupTruncateLogType.Truncate;

                    //string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
                    BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
                    backup.Devices.Add(backupItemDevice);
                    backup.PercentCompleteNotification = 10;

                    backup.SqlBackup(svr);
                    svr = null;
                    cn.Close();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message,ex);
            }
            return result;
        }
        public bool restoreDatabase(string fileName)
        {
            bool result = false;
            try
            {
                using (SqlConnection cn = new SqlConnection(data.ConnectString))
                {
                    ServerConnection svCon = new ServerConnection(cn);
                    Server svr = new Server(svCon);
                    cn.Open();
                    cn.ChangeDatabase("master");

                    //string testFolder = @"D:\";
                    string databaseName = "QuanLyGiaPha";
                    Restore restore = new Restore();
                    restore.Action = RestoreActionType.Database;
                    //string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
                    BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
                    restore.Devices.AddDevice(fileName, DeviceType.File);
                    restore.Database = databaseName;
                    restore.ReplaceDatabase = true;
                    restore.PercentCompleteNotification = 10;
                    svr.KillAllProcesses(databaseName);

                    restore.SqlRestore(svr);
                    svr.Refresh();
                    svr = null;
                    cn.Close();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message,ex);
            }
            return result;
        }
    }
}
